package com.example.zhaoping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhaoping.domain.Companies;
import com.example.zhaoping.domain.Jobs;
import com.example.zhaoping.domain.ResponseResult;
import com.example.zhaoping.mapper.JobsMapper;
import com.example.zhaoping.service.ICompaniesService;
import com.example.zhaoping.service.IJobsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhaoping.utils.BeanCopyUtils;
import com.example.zhaoping.vo.JobsVo;
import com.example.zhaoping.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 正在招聘的岗位表 服务实现类
 * </p>
 *
 * @author czx
 * @since 2024-12-25
 */
@Service
public class JobsServiceImpl extends ServiceImpl<JobsMapper, Jobs> implements IJobsService {
//===========================分页查询岗位列表====================================
@Autowired
private ICompaniesService companiesService;
    @Override
    public ResponseResult jobsList(Integer pageNum, Integer pageSize, String title) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        LambdaQueryWrapper<Jobs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(title),Jobs::getTitle,title);

        Page<Jobs> page = new Page<>(pageNum,pageSize);
        Page<Jobs> articlePage = page(page, queryWrapper);
        List<Jobs> records = articlePage.getRecords();
        List<JobsVo> jobsVos = BeanCopyUtils.copyBeanList(records, JobsVo.class);
        //把 jobsVos里面的name补充完整
        for (JobsVo jobsVo : jobsVos) {
            Companies companies = companiesService.getById(jobsVo.getCompanyId());
            jobsVo.setName(companies.getName());
        }



        PageVo pageVo = new PageVo(jobsVos, articlePage.getTotal());
        return ResponseResult.okResult(pageVo);

    }
    //===========================根据公司id查询岗位并且分页===========================

    @Override
    public ResponseResult getJobsById(Long id, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        LambdaQueryWrapper<Jobs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Jobs::getCompanyId,id);
        Page<Jobs> page = new Page<>(pageNum,pageSize);
        Page<Jobs> articlePage = page(page, queryWrapper);
        List<Jobs> records = articlePage.getRecords();
        List<JobsVo> jobsVos = BeanCopyUtils.copyBeanList(records, JobsVo.class);
        for (JobsVo jobsVo : jobsVos) {
            Companies companies = companiesService.getById(jobsVo.getCompanyId());
            jobsVo.setName(companies.getName());
        }
        PageVo pageVo = new PageVo(jobsVos, articlePage.getTotal());
        return ResponseResult.okResult(pageVo);

    }
}
